package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.RoleEnum;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wq.mapper.WqGameHomeMapper;
import com.ruoyi.wq.mapper.WqUserGameMapper;
import com.ruoyi.wq.domain.WqGameHome;
import com.ruoyi.wq.domain.vo.WqGameHomeVO;
import com.ruoyi.wq.domain.vo.WqGameRoleVO;
import com.ruoyi.wq.service.IWqGameHomeService;
import cn.hutool.core.util.StrUtil;

/**
 * 固玩组队Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
public class WqGameHomeServiceImpl extends ServiceImpl<WqGameHomeMapper, WqGameHome> implements IWqGameHomeService {

    @Autowired
    private WqGameHomeMapper wqGameHomeMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private WqUserGameMapper wqUserGameMapper;
    
    /**
     * 查询固玩组队
     * 
     * @param id 固玩组队主键
     * @return 固玩组队
     */
    @Override
    public WqGameHomeVO selectWqGameHomeById(String id){
    	WqGameHomeVO vo = baseMapper.getInfo(id);
    	
    	WqGameRoleVO shang = new WqGameRoleVO();
    	shang.setIsLock(true);
    	shang.setHomeId(id);
    	shang.setRole(RoleEnum.SHANG.getCode());
    	if(vo.getShangUserId() != null){
    		SysUser user = sysUserService.getById(vo.getShangUserId());
    		shang.setAvatar(user.getAvatar());
    		shang.setNickName(user.getGameName());
    		shang.setUserId(vo.getShangUserId());
    		shang.setSex(user.getSex());
    		shang.setAge(user.getAge());
    		shang.setIsHome(vo.getShangUserId().intValue() == vo.getUserId().intValue());
    		shang.setLevel(user.getGameLevel());
    		shang.setIsLock(false);
    	}else{
    		if(vo.getRoleLimit().contains(RoleEnum.SHANG.getCode())){
    			shang.setIsLock(false);
    		}
    	}
    	vo.setShang(shang);
    	
    	WqGameRoleVO zhong = new WqGameRoleVO();
    	zhong.setIsLock(true);
    	zhong.setHomeId(id);
    	zhong.setRole(RoleEnum.ZHONG.getCode());
    	if(vo.getZhongUserId() != null){
    		SysUser user = sysUserService.getById(vo.getZhongUserId());
    		zhong.setAvatar(user.getAvatar());
    		zhong.setNickName(user.getGameName());
    		zhong.setUserId(vo.getZhongUserId());
    		zhong.setSex(user.getSex());
    		zhong.setAge(user.getAge());
    		zhong.setIsHome(vo.getZhongUserId().intValue() == vo.getUserId().intValue());
    		zhong.setLevel(user.getGameLevel());
    		zhong.setIsLock(false);
    	}else{
    		if(vo.getRoleLimit().contains(RoleEnum.ZHONG.getCode())){
    			zhong.setIsLock(false);
    		}
    	}
    	vo.setZhong(zhong);
    	
    	WqGameRoleVO shoot = new WqGameRoleVO();
    	shoot.setIsLock(true);
    	shoot.setHomeId(id);
    	shoot.setRole(RoleEnum.SHOOT.getCode());
    	if(vo.getShootUserId() != null){
    		SysUser user = sysUserService.getById(vo.getShootUserId());
    		shoot.setAvatar(user.getAvatar());
    		shoot.setNickName(user.getGameName());
    		shoot.setUserId(vo.getShootUserId());
    		shoot.setSex(user.getSex());
    		shoot.setAge(user.getAge());
    		shoot.setIsHome(vo.getShootUserId().intValue() == vo.getUserId().intValue());
    		shoot.setLevel(user.getGameLevel());
    		shoot.setIsLock(false);
    	}else{
    		if(vo.getRoleLimit().contains(RoleEnum.SHOOT.getCode())){
    			shoot.setIsLock(false);
    		}
    	}
    	vo.setShoot(shoot);
    	
    	WqGameRoleVO fuzhu = new WqGameRoleVO();
    	fuzhu.setIsLock(true);
    	fuzhu.setHomeId(id);
    	fuzhu.setRole(RoleEnum.FUZHU.getCode());
    	if(vo.getFuzhuUserId() != null){
    		SysUser user = sysUserService.getById(vo.getFuzhuUserId());
    		fuzhu.setAvatar(user.getAvatar());
    		fuzhu.setNickName(user.getGameName());
    		fuzhu.setUserId(vo.getFuzhuUserId());
    		fuzhu.setSex(user.getSex());
    		fuzhu.setAge(user.getAge());
    		fuzhu.setIsHome(vo.getFuzhuUserId().intValue() == vo.getUserId().intValue());
    		fuzhu.setLevel(user.getGameLevel());
    		fuzhu.setIsLock(false);
    	}else{
    		if(vo.getRoleLimit().contains(RoleEnum.FUZHU.getCode())){
    			fuzhu.setIsLock(false);
    		}
    	}
    	vo.setFuzhu(fuzhu);
    	
    	WqGameRoleVO daye = new WqGameRoleVO();
    	daye.setIsLock(true);
    	daye.setHomeId(id);
    	daye.setRole(RoleEnum.DAYE.getCode());
    	if(vo.getDayeUserId() != null){
    		SysUser user = sysUserService.getById(vo.getDayeUserId());
    		daye.setAvatar(user.getAvatar());
    		daye.setNickName(user.getGameName());
    		daye.setUserId(vo.getDayeUserId());
    		daye.setSex(user.getSex());
    		daye.setAge(user.getAge());
    		daye.setIsHome(vo.getDayeUserId().intValue() == vo.getUserId().intValue());
    		daye.setLevel(user.getGameLevel());
    		daye.setIsLock(false);
    	}else{
    		if(vo.getRoleLimit().contains(RoleEnum.DAYE.getCode())){
    			daye.setIsLock(false);
    		}
    	}
    	vo.setDaye(daye);
    	
    	
    	Integer sumNum = 0;
		String[] roleLimitList = vo.getRoleLimit().split("/");
		List<String> roleList = new ArrayList<String>();
		for(String role : roleLimitList){
			roleList.add(role);
		}
		if(vo.getShangUserId() != null){
			roleList.remove(RoleEnum.SHANG.getCode());
			sumNum ++;
		}
		if(vo.getShootUserId() != null){
			roleList.remove(RoleEnum.SHOOT.getCode());
			sumNum ++;
		}
		if(vo.getDayeUserId() != null){
			roleList.remove(RoleEnum.DAYE.getCode());
			sumNum ++;
		}
		if(vo.getFuzhuUserId() != null){
			roleList.remove(RoleEnum.FUZHU.getCode());
			sumNum ++;
		}
		if(vo.getZhongUserId() != null){
			roleList.remove(RoleEnum.ZHONG.getCode());
			sumNum ++;
		}
		vo.setRoleLimit(StrUtil.join("/", roleList));
		vo.setSumNum(sumNum);
    	return vo;
    }

    /**
     * 查询固玩组队列表
     * 
     * @param wqGameHome 固玩组队
     * @return 固玩组队
     */
    @Override
    public List<WqGameHome> selectWqGameHomeList(WqGameHome wqGameHome)
    {
        return wqGameHomeMapper.selectWqGameHomeList(wqGameHome);
    }

    /**
     * 修改固玩组队
     * 
     * @param wqGameHome 固玩组队
     * @return 结果
     */
    @Override
    public int updateWqGameHome(WqGameHome wqGameHome)
    {
        wqGameHome.setUpdateTime(DateUtils.getNowDate());
        return wqGameHomeMapper.updateWqGameHome(wqGameHome);
    }

    /**
     * 批量删除固玩组队
     * 
     * @param ids 需要删除的固玩组队主键
     * @return 结果
     */
    @Override
    public int deleteWqGameHomeByIds(String[] ids)
    {
        return wqGameHomeMapper.deleteWqGameHomeByIds(ids);
    }

    /**
     * 删除固玩组队信息
     * 
     * @param id 固玩组队主键
     * @return 结果
     */
    @Override
    public int deleteWqGameHomeById(String id)
    {
        return wqGameHomeMapper.deleteWqGameHomeById(id);
    }

	@Override
	public TableDataInfo getPageList(WqGameHomeVO wqGameHome) {
		IPage<WqGameHomeVO> page = baseMapper.selectPageList(Condition.getPage(wqGameHome), wqGameHome);
		for(WqGameHomeVO vo : page.getRecords()){
			Integer sumNum = 0;
			String[] roleLimitList = vo.getRoleLimit().split("/");
			List<String> roleList = new ArrayList<String>();
			for(String role : roleLimitList){
				roleList.add(role);
			}
			if(vo.getShangUserId() != null){
				roleList.remove("对抗路");
				sumNum ++;
			}
			if(vo.getShootUserId() != null){
				roleList.remove("发育路");
				sumNum ++;
			}
			if(vo.getDayeUserId() != null){
				roleList.remove("打野");
				sumNum ++;
			}
			if(vo.getFuzhuUserId() != null){
				roleList.remove("游走");
				sumNum ++;
			}
			if(vo.getZhongUserId() != null){
				roleList.remove("中路");
				sumNum ++;
			}
			vo.setRoleLimit(StrUtil.join("/", roleList));
			vo.setSumNum(sumNum);
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public void removeHome(WqGameHomeVO wqGameHome) {
		baseMapper.removeHome(wqGameHome);
	}

}
